package org.jsrml.sv.mob.controller.base;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.jsrml.common.pagination.Pagination;
import org.jsrml.common.util.JSONResultDTO;
import org.jsrml.core.base.command.SaveAuthAccountCo;
import org.jsrml.core.base.entity.AuthAccount;
import org.jsrml.core.base.qo.AuthAccountQO;
import org.jsrml.core.base.service.AuthAccountService;
import org.jsrml.sv.mob.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value = "/authAccount")
public class AuthAccountController extends BaseController{
	
	@Autowired
	private AuthAccountService authAccountService;
	
	@RequestMapping("/query")
	@ResponseBody
	public JSONResultDTO query(HttpServletRequest request, @RequestBody AuthAccountQO qo) {
		try {
			switch (qo.getResultType()) {
			case 0:
				Integer count = authAccountService.queryCount(qo);
				return createSuccessResult(count);
			case 1:
				AuthAccount authAccount = authAccountService.queryUnique(qo);
				return createSuccessResult(authAccount);
			case 2:
				List<AuthAccount> list = authAccountService.queryList(qo);
				return createSuccessResult(list);
			case 3:
				Pagination pagination = authAccountService.queryPagination(qo);
				return createSuccessResult(pagination);
			default:
				return createFailResult("-1", "参数中缺少正确的resultType", null);
			}
		} catch (Exception e) {
			return createFailResult("-1", e.getMessage(), null);
		}
	}
	
	
	/**
	 * 修改账号
	 */
	@RequestMapping("/modify")
	@ResponseBody
	public JSONResultDTO modify(HttpServletRequest request,
			@RequestBody SaveAuthAccountCo command) {

		AuthAccount authAccount = null;
		try{
			authAccount = authAccountService.modifyAuthAccount(command);
		}catch (Exception e) {
			return createFailResult("-1", e.getMessage(), null);
		}
		return createSuccessResult(authAccount);
	}
	
}
